package com.tencent.wxcloudrun.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

@RestController
@RequestMapping("/phpdir")
public class YingGaoController {


    @GetMapping("/trade_third.php")
    public Object handleTradeThird(
            @RequestParam("tradecode") String tradeCode,
            @RequestParam("login") String login,
            @RequestParam("pass") String pass,
            @RequestParam("con_type") Integer conType, HttpServletResponse response) throws IOException {


        if (tradeCode.equals("networkclient")) {
            if (!login.equals("thirdadmin")){
                throw new RuntimeException("用户名或密码 不正确");
            }
            if (!pass.equals("888888")){
                throw new RuntimeException("用户名或密码 不正确");
            }
            if(conType == 1){
                ObjectMapper objectMapper = new ObjectMapper();
                // 获取 resources/doc 目录下的 JSON 文件路径
                // 睡眠10秒
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                File file = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("doc/data.json")).getFile());
                return objectMapper.readValue(file, Object.class);
            }
            if(conType == 2){
                response.setContentType("application/xml;charset=gbk");
                return "<?xml version=\"1.0\" encoding=\"gbk\"?>\n" +
                        "<rows>\n" +
                        "    <cell name='AllNum' >\n" +
                        "        <![CDATA[4979]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='WaitAuditNum' >\n" +
                        "        <![CDATA[5]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='RegNum' >\n" +
                        "        <![CDATA[4360]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='DayNum' >\n" +
                        "        <![CDATA[6]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='MonthNum' >\n" +
                        "        <![CDATA[294]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='UnlineNum' >\n" +
                        "        <![CDATA[371]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='PassNum' >\n" +
                        "        <![CDATA[2291]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='RepairNum' >\n" +
                        "        <![CDATA[974]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='FalseNum' >\n" +
                        "        <![CDATA[119]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='AllRepairNum' >\n" +
                        "        <![CDATA[2103]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='DayRepairNum' >\n" +
                        "        <![CDATA[62]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='MonthRepairNum' >\n" +
                        "        <![CDATA[1399]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='CheckTimes' >\n" +
                        "        <![CDATA[237398]]>\n" +
                        "    </cell>\n" +
                        "    <cell name='RepairTimes' >\n" +
                        "        <![CDATA[7062]]>\n" +
                        "    </cell>\n" +
                        "</rows>";
            }
        }else {
            return "";
        }
        return "";
    }


}
